﻿@using MVCControlsToolkit.Controls
@using TamoCRM.Domain.Schools
@using TamoCRM.Web.Mvc.Areas.Admin.Controllers
@model TamoCRM.Web.Mvc.Areas.Admin.Models.Contacts.ContactCreateModel
@{
    ViewBag.Title = "Add";
    Layout = "~/Areas/Admin/Views/Shared/_Layout.cshtml";
}
@{
    List<TamoCRM.Domain.Status.StatusInfo> status = ViewBag.Status;
    List<TamoCRM.Domain.Collectors.CollectorInfo> collectors = ViewBag.Collectors;
    List<TamoCRM.Domain.Channels.ChannelInfo> channels = ViewBag.Channels;
    //List<TamoCRM.Domain.SourceTypes.SourceTypeInfo> sourceTypes = ViewBag.SourceTypes;
    List<TamoCRM.Domain.Levels.LevelInfo> levels = ViewBag.Levels;
    //List<TamoCRM.Domain.Branches.BranchInfo> branches = ViewBag.Branches;
    List<ContactController.GenderInfo> genders = ViewBag.Genders;
    List<SchoolInfo> schools = ViewBag.Schools;
    var lstCollectors = new SelectList(collectors, "CollectorId", "Name");
    var lstChannels = new SelectList(channels, "ChannelId", "Name");
    //var lstSourceTypes = new SelectList(sourceTypes, "SourceTypeId", "Name");
    var lstLevels = new SelectList(levels, "LevelId", "Name");
    //var lstBranches = new SelectList(branches, "BranchId", "Name");
    var lstGenders = new SelectList(genders, "Code", "Name");
    var lstSchools = new SelectList(schools, "SchoolId", "Name");
    var lstStatus = new SelectList(status, "StatusId", "Name");
}
<div class="breadcrumbs" id="breadcrumbs">
    <script type="text/javascript">
        try { ace.settings.check('breadcrumbs', 'fixed') } catch (e) { }
    </script>

    <ul class="breadcrumb">
        <li>
            <i class="icon-home home-icon"></i>
            <a href="/Home">Trang chủ</a>
        </li>

        <li>
            <a href="javascript:void(0);">Danh mục</a>
        </li>
        <li class="active">Contact</li>
    </ul>
</div>
<div class="page-content">
    <div class="page-header">
        <h1>Thêm mới contact								
        </h1>
    </div>
    <!-- /.page-header -->

    <div class="row">
        <div class="col-xs-12">
            <span class="label label-success arrowed" id="spMessage"></span>
        </div>
        <div class="col-xs-12">
            @using (Ajax.BeginForm("Create", "Contact", new AjaxOptions() { HttpMethod = "POST" }, new { enctype = "multipart/form-data", @class = "form-horizontal", role = "form", id = "createcontact" }))
            {
                @Html.AntiForgeryToken()
                <div class="row">
                    <div class="col-xs-2">
                        Tên
                    </div>
                    <div class="col-xs-4">
                        <div class="clearfix">
                            @Html.TextBoxFor(x => x.Fullname, new { @style = "width: 100%", @required = "" })
                        </div>
                    </div>
                    <div class="col-xs-2">
                        Giới tính  
                    </div>
                    <div class="col-xs-4">
                        @Html.DropDownListFor(x => x.Gender, lstGenders, new { @class = "form-control" })

                    </div>
                </div>
                <div class="space-4"></div>
                <div class="row">
                    <div class="col-xs-2">
                        Số điện thoại 1
                    </div>
                    <div class="col-xs-4">
                        <div class="input-group">
                            <span class="input-group-addon">
                                <i class="icon-phone"></i>
                            </span>@Html.TextBoxFor(x => x.Mobile1, new { @required = "" })
                        </div>
                    </div>
                    <div class="col-xs-2">
                        Email  
                    </div>
                    <div class="col-xs-4">
                        <div class="clearfix">
                            @Html.TextBoxFor(x => x.Email, new { type = "email", style = "width: 90%" })
                        </div>
                    </div>
                </div>
                <div class="space-4"></div>
                <div class="row">
                    <div class="col-xs-2">
                        Số điện thoại 2 
                    </div>
                    <div class="col-xs-4">
                        <div class="input-group">
                            <span class="input-group-addon">
                                <i class="icon-phone"></i>
                            </span>@Html.TextBoxFor(x => x.Mobile2)
                        </div>
                    </div>
                    @*<div class="col-xs-2">
                            Email 2
                        </div>
                        <div class="col-xs-2">
                            @Html.TextBoxFor(x => x.Email1)
                        </div>*@
                </div>
                <div class="space-4"></div>
                <div class="row">
                    <div class="col-xs-2">
                        Ngày sinh
                    </div>
                    <div class="col-xs-4">@Html.EditorFor(x => x.Birth)
                    </div>
                    <div class="col-xs-2">
                        Trạng thái
                    </div>
                    <div class="col-xs-4">
                        <div class="clearfix">
                            @Html.DropDownListFor(x => x.Status, lstStatus, "--", new { @class = "form-control", @required = "" })
                        </div>
                    </div>
                </div>
                <div class="space-4"></div>
                <div class="row">
                    <div class="col-xs-2">
                        Người lấy
                    </div>
                    <div class="col-xs-4">
                        <div class="clearfix">
                            @Html.DropDownListFor(x => x.CollectorId, lstCollectors, "--", new { @class = "form-control", @required = "" })
                        </div>
                    </div>
                    <div class="col-xs-2">
                        Kênh
                    </div>
                    <div class="col-xs-4">
                        <div class="clearfix">
                            @Html.DropDownListFor(x => x.ChannelId, lstChannels, "--", new { @class = "form-control", @required = "" })
                        </div>
                    </div>
                </div>
                <div class="space-4"></div>
                <div class="row">
                    <div class="col-xs-2">
                        Trường học
                    </div>
                    <div class="col-xs-4">
                        <div class="clearfix">
                            @Html.DropDownListFor(x => x.SchoolId, lstSchools, "--", new { @class = "form-control", @required = "" })
                        </div>
                    </div>
                    <div class="col-xs-2">
                        Level
                    </div>
                    <div class="col-xs-4">
                        <div class="clearfix">
                            @Html.DropDownListFor(x => x.LevelId, lstLevels, "--", new { @class = "form-control", @required = "" })
                        </div>
                    </div>
                </div>
                <div class="space-4"></div>
                <div class="row">
                    <div class="col-xs-2">
                        Ghi chú
                    </div>
                    <div class="col-xs-10">
                        @Html.TextAreaFor(x => x.Notes, new { @class = "autosize-transition form-control" })
                    </div>
                </div> 
                <div class="space-4"></div>
                <div class="row">
                    <div class="col-xs-12">
                        <input type="button" value="Lưu" id="btnSave" class="btn btn-sm btn-primary" />
                        <input type="button" value="Kiểm tra trùng" id="btnCheck" class="btn btn-sm btn-primary" />
                        <input type="hidden" id="duplicateContactId" value="" />
                    </div>
                </div> 
                <script>
                    $('#ContactInfo_Birth').datepicker({ format: 'dd/mm/yyyy', autoclose: true });
                </script>
            }
        </div>
        <!-- /.col -->
    </div>
    <!-- /.row -->
</div>
<script src="/Content/themes/ace/assets/js/jquery.form.js"></script>
<script src="/Content/themes/ace/assets/js/jquery.validate.min.js"></script>
<script src="/Content/themes/ace/assets/js/additional-methods.min.js"></script>


<script type="text/javascript">
    function checkDuplicate() {
        $.ajax({
            type: "POST",
            url: "/admin/api/contact/checkduplicate",
            data: {
                mobile1: $("#Mobile1").val(),
                mobile2: $("#Mobile2").val(),
                tel: "",
                email: $("#Email").val()
            },
            success: function (data) {
                $("#duplicateContactId").val(data);
            },
            complete: function (xhr) {
                
            },
            dataType: "json"
        });
        return parseInt($("#duplicateContactId").val());
    }
    $(document).ready(function () {
        $("#btnSave").click(function (e) {
            e.preventDefault();
            if (checkDuplicate() == 0) {
                $('#createcontact').submit();
            } else {
                alert("Hãy kiểm tra trùng trước khi lưu");
            }
        });
        $("#btnCheck").click(function (e) {
            e.preventDefault();
            checkDuplicate();
        });
        $('#createcontact').ajaxForm({
            beforeSubmit: function () {

                $("#spMessage").html('').hide();
                return $('#createcontact').valid();
            },
            uploadProgress: function (event, position, total, percentComplete) {
            },
            success: function () {
            },
            complete: function (xhr) {
                $("#spMessage").html(xhr.responseText).show();
            }
        });
        //$('#Mobile1,#Mobile').mask('9999999999999999');

        $('#createcontact').validate({
            errorElement: 'div',
            errorClass: 'help-block',
            focusInvalid: true,

            rules: {
                Email: {
                    email: true
                },
                //Mobile: {
                //    required: true,
                //    phone: 'required'
                //},
            },

            messages: {
                Email: {
                    email: "(*)"
                },
            },

            invalidHandler: function (event, validator) { //display error alert on form submit   
                //$('.alert-danger', $('.login-form')).show();
            },

            highlight: function (e) {
                //$(e).closest('.form-group').removeClass('has-info').addClass('has-error');
            },

            success: function (e) {
                //$(e).closest('.form-group').removeClass('has-error').addClass('has-info');
                //$(e).remove();
            },

            errorPlacement: function (error, element) {
                if (element.is(':checkbox') || element.is(':radio')) {
                    var controls = element.closest('div[class*="col-"]');
                    if (controls.find(':checkbox,:radio').length > 1) controls.append(error);
                    else error.insertAfter(element.nextAll('.lbl:eq(0)').eq(0));
                }
                else if (element.is('.select2')) {
                    error.insertAfter(element.siblings('[class*="select2-container"]:eq(0)'));
                }
                else if (element.is('.chosen-select')) {
                    error.insertAfter(element.siblings('[class*="chosen-container"]:eq(0)'));
                }
                else error.insertAfter(element.parent());
            },

            submitHandler: function (form) {

            },
            invalidHandler: function (form) {
            }
        });

    });
</script>
